unit UCadastroPermissao;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UFomCadastro, DBCtrls, StdCtrls, Buttons, ExtCtrls, Mask, DB,
  DBTables;

type
  TFCadastroPermissao = class(TFormCadastro)
    dsPermissao: TDataSource;
    Label1: TLabel;
    DBEdit1: TDBEdit;
    Label2: TLabel;
    DBEdit2: TDBEdit;
    Label3: TLabel;
    DBEdit3: TDBEdit;
    Label4: TLabel;
    DBEdit4: TDBEdit;
    QPermissao: TQuery;
    QPermissaoPER_CODIGO: TIntegerField;
    QPermissaoPER_MODULO: TStringField;
    QPermissaoPER_TRANSACAO: TStringField;
    QPermissaoPER_DESCRICAO: TStringField;
    procedure BtnIncluirClick(Sender: TObject);
    procedure BtnEditarClick(Sender: TObject);
    procedure BtnExcluirClick(Sender: TObject);
    procedure BtnSalvarClick(Sender: TObject);
    procedure BtnCancelarClick(Sender: TObject);
    procedure BtnFecharClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FCadastroPermissao: TFCadastroPermissao;

implementation

uses UDmGestao, UGeral;

{$R *.dfm}

procedure TFCadastroPermissao.BtnIncluirClick(Sender: TObject); 
begin
  dsPermissao.DataSet.Insert;
  inherited;
end;

procedure TFCadastroPermissao.BtnEditarClick(Sender: TObject);
begin
  inherited;
  dsPermissao.DataSet.Edit;
end;

procedure TFCadastroPermissao.BtnExcluirClick(Sender: TObject);
begin
  inherited;
  dsPermissao.DataSet.Delete;
end;

procedure TFCadastroPermissao.BtnSalvarClick(Sender: TObject);
var
  c : integer;
  cond : boolean;
begin
  if dsPermissao.DataSet.State = dsInsert then
  begin
    c := 0;
    cond := false;
    while not(cond) do
    begin
      try
        dsPermissao.DataSet.FieldByName('PER_CODIGO').AsInteger :=
        dsPermissao.DataSet.RecordCount + (c + 1);
        dsPermissao.DataSet.post;
      except
        inc(c);
        Continue;
      end;
      cond := true;
    end;
  end else
    dsPermissao.DataSet.post;
  inherited;
end;

procedure TFCadastroPermissao.BtnCancelarClick(Sender: TObject);
begin
  inherited;
  dsPermissao.DataSet.Cancel;
end;

procedure TFCadastroPermissao.BtnFecharClick(Sender: TObject);
begin
  inherited;
  close;
end;

procedure TFCadastroPermissao.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
  dsPermissao.DataSet.Close;
  FCadastroPermissao := nil;
end;

procedure TFCadastroPermissao.FormCreate(Sender: TObject);
begin
  inherited;
  if not(dsPermissao.DataSet.Active) then
    dsPermissao.DataSet.Open;
end;

end.
